home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1991: Code Warrior / bincue / Code Warrior.bin / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell / DoEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-21  |  7.8 KB  |  350 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:        CShell
  5. ** File:        doevent.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  19. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  21.  
  22. #ifndef __DESK__
  23. #include <Desk.h>
  24. #endif
  25.  
  26. #ifndef __DISKINIT__
  27. #include <DiskInit.h>
  28. #endif
  29.  
  30. #ifndef __ERRORS__
  31. #include <Errors.h>
  32. #endif
  33.  
  34. #ifndef __MENUS__
  35. #include <Menus.h>
  36. #endif
  37.  
  38. #ifndef __TEXTEDITCONTROL__
  39. #include <TextEditControl.h>
  40. #endif
  41.  
  42. #ifndef __TOOLUTILS__
  43. #include <ToolUtils.h>
  44. #endif
  45.  
  46. #ifndef __UTILITIES__
  47. #include <Utilities.h>
  48. #endif
  49.  
  50.  
  51.  
  52. /*****************************************************************************/
  53.  
  54.  
  55.  
  56. extern Cursor    *gCurrentCursor;
  57.  
  58.  
  59.  
  60. /*****************************************************************************/
  61. /*****************************************************************************/
  62.  
  63.  
  64.  
  65. /* Do the right thing for an event.  Determine what kind of event it is, and
  66. ** call the appropriate routines.
  67. */
  68.  
  69. #pragma segment Main
  70. void    DoEvent(EventRecord *event)
  71. {
  72.     short            part, err;
  73.     WindowPtr        window;
  74.     char            key;
  75.     Point            pt;
  76.     Rect            contentRct, oldRct, growLimits;
  77.     long            size;
  78.     FileRecHndl        frHndl;
  79.  
  80.     switch(event->what) {
  81.  
  82.         case mouseDown:
  83.             gCurrentCursor = nil;
  84.                 /* No shortcuts when we recalculate the cursor region. */
  85.  
  86.             part = FindWindow(event->where, &window);
  87.             if (part != inContent) DoSetCursor(&qd.arrow);
  88.  
  89.             switch(part) {
  90.  
  91.                 case inContent:
  92.                     if (window != FrontWindow()) {
  93.                         SelectWindow(window);
  94.                         if (IsAppWindow(window)) {
  95.                             DoUpdate(window);
  96.                             contentRct = GetWindowContentRect(window);
  97.                             if (PtInRect(event->where, &contentRct))
  98.                                 DoEvent(event);
  99.                         }
  100.                                     /* Do first click if over board. */
  101.                     } else
  102.                         DoContentClick(window, event);
  103.                     break;
  104.  
  105.                 case inDrag:            
  106.                     DragWindow(window, event->where, &qd.screenBits.bounds);
  107.                     break;        /* Pass screenBits.bounds to
  108.                                 ** get all gDevices. */
  109.  
  110.                 case inGoAway:
  111.                     if (TrackGoAway(window, event->where)) {
  112.                         DisposeOneWindow(window, iClose);
  113.                     }
  114.                     break;
  115.  
  116.                 case inGrow:
  117.                     oldRct = GetWindowContentRect(window);
  118.                     SetRect(&growLimits, kMinWindowWidth, kMinWindowHeight,
  119.                                          kMaxWindowWidth, kMaxWindowHeight);
  120.                     if (size = GrowWindow(window, event->where, &growLimits)) {
  121.                         pt = *(Point *)&size;
  122.                         SizeWindow(window, pt.h, pt.v, true);
  123.                         ResizeContent(window);
  124.                     }
  125.                     break;
  126.  
  127.                 case inMenuBar:        /* Process mouse menu command (if any). */
  128.                     AdjustMenus();
  129.                     DoMenuCommand(MenuSelect(event->where));
  130.                     break;
  131.  
  132.                 case inSysWindow:    /* Let the system handle the mouseDown. */
  133.                     SystemClick(event, window);
  134.                     break;
  135.  
  136.                 case inZoomIn:
  137.                 case inZoomOut:
  138.                     if (TrackBox(window, event->where, part)) {
  139.                         ZoomToWindowDevice(window, 640, 400, part, true);
  140.                         ResizeContent(window);
  141.                     }
  142.                     break;
  143.  
  144.             }
  145.             break;
  146.  
  147.         case activateEvt:
  148.             gCurrentCursor = nil;
  149.                 /* No shortcuts when we recalculate the cursor region. */
  150.  
  151.             DoActivate((WindowPtr)event->message,
  152.                        (event->modifiers & activeFlag));
  153.             break;
  154.  
  155.         case autoKey:
  156.         case keyDown:                    /* Check for menukey equivalents. */
  157.             key = event->message & charCodeMask;
  158.             if (event->modifiers & cmdKey) {        /* Command key down. */
  159.                 if (event->what == keyDown) {
  160.                     AdjustMenus();
  161.                         /* Enable/disable/check menu items properly. */
  162.                     DoMenuCommand(MenuKey(key));
  163.                 }
  164.                 break;
  165.             }
  166.  
  167.             if (!IsAppWindow(window = FrontWindow())) break;
  168.             frHndl = (FileRecHndl)GetWRefCon(window);
  169.  
  170.             if (key == 0x03) {
  171.                 SendMessage(frHndl, kTextMssg);
  172.                 break;
  173.             }
  174.             if (CTEKey(event)) {
  175.                 (*frHndl)->fileState.docDirty = true;
  176.                 AdjustMenus();        /* Avoid unnecessary DoCursor() and speed */
  177.                 return;                /* up TextEdit entry. */
  178.             }
  179.             break;
  180.  
  181.         case diskEvt:
  182.             gCurrentCursor = nil;
  183.                 /* No shortcuts when we recalculate the cursor region. */
  184.  
  185.             if (HiWord(event->message) != noErr) {
  186.                 SetPt(&pt, kDILeft, kDITop);
  187.                 err = DIBadMount(pt, event->message);
  188.             }
  189.             break;        /* It is not a bad idea to at least call DIBadMount
  190.                         ** in response to a diskEvt, so that the user can
  191.                         ** format a floppy.
  192.                         */
  193.         case kHighLevelEvent:
  194.             gCurrentCursor = nil;
  195.                 /* No shortcuts when we recalculate the cursor region. */
  196.  
  197.             DoHighLevelEvent(event);
  198.             break;
  199.  
  200.         case kOSEvent:
  201.             gCurrentCursor = nil;
  202.                 /* No shortcuts when we recalculate the cursor region. */
  203.  
  204.             switch ((event->message >> 24) & 0x0FF) {
  205.                     /* Must logical and with 0x0FF to get only low byte. */
  206.                     /* High byte of message. */
  207.  
  208.                 case kMouseMovedMessage:
  209.                     break;
  210.  
  211.                 case kSuspendResumeMessage:
  212.                         /* Suspend/resume is also an activate/deactivate. */
  213.                     gInBackground = !(event->message & kResumeMask);
  214.                     DoActivate(FrontWindow(), !gInBackground);
  215.                     break;
  216.             }
  217.             break;
  218.  
  219.         case updateEvt:
  220.             DoUpdate((WindowPtr)event->message);
  221.             break;
  222.  
  223.     }
  224.  
  225.     DoCursor(false, 0);
  226.     AdjustMenus();
  227. }
  228.  
  229.  
  230.  
  231. /*****************************************************************************/
  232.  
  233.  
  234.  
  235. /* This is called when a window is activated or deactivated. */
  236.  
  237. #pragma segment Main
  238. void    DoActivate(WindowPtr window, Boolean becomingActive)
  239. {
  240.     NotifyCancel();
  241.  
  242.     if (IsAppWindow(window)) {
  243.         SetPort(window);
  244.         CTEWindActivate(window, becomingActive);
  245.         DoDrawGrowIcon(window, false, false);    /* Just draw the icon. */
  246.         DoDrawControls(window, true);            /* Redraw just the scrollbars. */
  247.         SetOrigin(0, 0);
  248.     }
  249. }
  250.  
  251.  
  252.  
  253. /*****************************************************************************/
  254.  
  255.  
  256.  
  257. /* This is called when an update event is received for a window.  It calls
  258. ** ImageDocument to draw the contents of an application window.  As an
  259. ** effeciency measure that does not have to be followed, it calls the drawing
  260. ** routine only if the visRgn is non-empty.  This will handle situations where
  261. ** calculations for drawing or drawing itself is very time-consuming.
  262. */
  263.  
  264. #pragma segment Main
  265. void    DoUpdate(WindowPtr window)
  266. {
  267.     if (IsAppWindow(window)) {
  268.         BeginUpdate(window);                /* This sets up the visRgn. */
  269.         if (!EmptyRgn(window->visRgn)) {    /* Draw if updating needs doing. */
  270.             SetPort(window);
  271.             ImageDocument((FileRecHndl)GetWRefCon(window));
  272.         }
  273.         EndUpdate(window);
  274.     }
  275. }
  276.  
  277.  
  278.  
  279. /*****************************************************************************/
  280.  
  281.  
  282.  
  283. /* This is called when a mouse-down event occurs in the content of a window.
  284. ** Other applications might want to call FindControl, TEClick, etc., to
  285. ** further process the click.
  286. */
  287.  
  288. #pragma segment Main
  289. void    DoContentClick(WindowPtr window, EventRecord *event)
  290. {
  291.     Point        clickLoc;
  292.     FileRecHndl    frHndl;
  293.  
  294.     if (!IsAppWindow(window)) return;
  295.  
  296.     SetPort(window);
  297.  
  298.     clickLoc = event->where;
  299.     GlobalToLocal(&clickLoc);
  300.  
  301.     if (CTEClick(event)) return;
  302.         /* If TextEdit control handled the click, we are done. */
  303.  
  304.     frHndl = (FileRecHndl)GetWRefCon(window);
  305.     if ((*frHndl)->fileState.readOnly) return;
  306.         /* Don't allow changes if read-only. */
  307. }
  308.  
  309.  
  310.  
  311. /*****************************************************************************/
  312.  
  313.  
  314.  
  315. #pragma segment Main
  316. void    ResizeContent(WindowPtr window)
  317. {
  318.     FileRecHndl    frHndl;
  319.     WindowPtr    oldPort;
  320.     short        i;
  321.     Rect        rct;
  322.     TEHandle    teHndl;
  323.  
  324.     frHndl  = (FileRecHndl)GetWRefCon(window);
  325.     oldPort = SetFilePort(frHndl);
  326.  
  327.     for (i = 0; i < 2; ++i) {
  328.         rct = window->portRect;
  329.         --rct.top;
  330.         --rct.left;
  331.         rct.right -= 14;
  332.         rct.bottom = rct.top + (rct.bottom - rct.top + 1) / 2;
  333.         if (i) {
  334.             rct.top = rct.bottom - 1;
  335.             rct.bottom = window->portRect.bottom + 1;
  336.         }
  337.  
  338.         teHndl = (*frHndl)->doc.inBox;
  339.         if (i) teHndl = (*frHndl)->doc.outBox;
  340.  
  341.         CTESize(teHndl, rct.right - rct.left, rct.bottom - rct.top);
  342.         CTEMove(teHndl, rct.left, rct.top);
  343.     }
  344.  
  345.     SetPort(oldPort);
  346. }
  347.  
  348.  
  349.  
  350.